﻿using System;

namespace DataAccess.SqlServer
{
    public class SqlValueTypeConverter : IValueTypeConverter
    {
        /// <summary>
        /// Converts value type.
        /// DBNull to null
        /// Decimal to single
        /// </summary>
        /// <param name="type">property type</param>
        /// <param name="obj">object</param>
        /// <returns>object</returns>
        public object Convert(Type type, object obj)
        {
            if (obj == DBNull.Value)
            {
                // Default value
                if (typeof(int).Equals(type))
                {
                    return -1;
                }
                else if (typeof(DateTime).Equals(type))
                {
                    return DateTime.Now;
                }
                else if (typeof(string).Equals(type))
                {
                    return string.Empty;
                }
                else 
                {
                    return null;
                }
            }

            // Convert type
            if (typeof(Decimal).Equals(obj.GetType()))
            {
                obj = Decimal.ToSingle((Decimal)obj);
            }

            return obj;
        }
    }
}
